package com.china08.yxyapi.service.yxyresource.impl;

import org.apache.commons.lang3.StringUtils;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder.Type;
import org.elasticsearch.index.query.RegexpQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.SystemConstant;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.service.yxyresource.YxyResourceBaseService;

@Service
public class YxyResourceBaseServiceImpl implements YxyResourceBaseService {

    @Autowired
    private ElasticsearchTemplate et;

    @Override
    public void keywordT(String keyword, Integer type, BoolQueryBuilder builder) {
        if (StringUtils.isNotBlank(keyword) && keyword.length() == 1) {
            builder.must(new RegexpQueryBuilder("title",
                    SystemConstant.RESOURCE_PREFIX + QueryParser.escape(keyword) + SystemConstant.RESOURCE_SUFFIX));
        } else {
            if (type == 1) {
                builder.must(new MatchQueryBuilder("title", keyword).type(Type.PHRASE));
            } else if (type == 2) {
                builder.must(new MatchQueryBuilder("title", keyword));
            } else if (type == 4) {
                builder.must(new RegexpQueryBuilder("title",
                        SystemConstant.RESOURCE_PREFIX + QueryParser.escape(keyword) + SystemConstant.RESOURCE_SUFFIX));
            } else {
                BoolQueryBuilder keywordBoolQueryBuilder = new BoolQueryBuilder();
                String[] key = keyword.split("");
                for (String c : key) {
                    keywordBoolQueryBuilder.should(new RegexpQueryBuilder("title",
                            SystemConstant.RESOURCE_PREFIX + QueryParser.escape(c) + SystemConstant.RESOURCE_SUFFIX));
                }
                builder.must(keywordBoolQueryBuilder);
            }
        }
    }

    @Override
    public void keywordTU(String keyword, Integer type, BoolQueryBuilder builder) {
        if (StringUtils.isNotBlank(keyword) && keyword.length() == 1) {
            BoolQueryBuilder b = new BoolQueryBuilder();
            b.should(new RegexpQueryBuilder("title",
                    SystemConstant.RESOURCE_PREFIX + QueryParser.escape(keyword) + SystemConstant.RESOURCE_SUFFIX));
            b.should(new RegexpQueryBuilder("uploaderName",
                    SystemConstant.RESOURCE_PREFIX + QueryParser.escape(keyword) + SystemConstant.RESOURCE_SUFFIX));
            builder.must(b);
        } else {
            if (type == 1) {
                BoolQueryBuilder b = new BoolQueryBuilder();
                b.should(new MatchQueryBuilder("title", keyword).type(Type.PHRASE));
                b.should(new MatchQueryBuilder("uploaderName", keyword).type(Type.PHRASE));
                builder.must(b);
            } else if (type == 2) {
                BoolQueryBuilder b = new BoolQueryBuilder();
                b.should(new MatchQueryBuilder("title", keyword));
                b.should(new MatchQueryBuilder("uploaderName", keyword));
                builder.must(b);
            } else if (type == 4) {
                BoolQueryBuilder b = new BoolQueryBuilder();
                b.should(new RegexpQueryBuilder("title",
                        SystemConstant.RESOURCE_PREFIX + QueryParser.escape(keyword) + SystemConstant.RESOURCE_SUFFIX));
                b.should(new RegexpQueryBuilder("uploaderName",
                        SystemConstant.RESOURCE_PREFIX + QueryParser.escape(keyword) + SystemConstant.RESOURCE_SUFFIX));
                builder.must(b);
            } else {
                BoolQueryBuilder keywordBoolQueryBuilder = new BoolQueryBuilder();
                String[] key = keyword.split("");
                for (String c : key) {
                    keywordBoolQueryBuilder.should(new RegexpQueryBuilder("title",
                            SystemConstant.RESOURCE_PREFIX + QueryParser.escape(c) + SystemConstant.RESOURCE_SUFFIX));
                    keywordBoolQueryBuilder.should(new RegexpQueryBuilder("uploaderName",
                            SystemConstant.RESOURCE_PREFIX + QueryParser.escape(c) + SystemConstant.RESOURCE_SUFFIX));
                }
                builder.must(keywordBoolQueryBuilder);
            }
        }
    }

    @Override
    public void keywordTUS(String keyword, Integer type, BoolQueryBuilder builder) {
        if (StringUtils.isNotBlank(keyword) && keyword.length() == 1) {
            BoolQueryBuilder b = new BoolQueryBuilder();
            b.should(new RegexpQueryBuilder("title",
                    SystemConstant.RESOURCE_PREFIX + QueryParser.escape(keyword) + SystemConstant.RESOURCE_SUFFIX));
            b.should(new RegexpQueryBuilder("uploaderName",
                    SystemConstant.RESOURCE_PREFIX + QueryParser.escape(keyword) + SystemConstant.RESOURCE_SUFFIX));
            b.should(new RegexpQueryBuilder("uploaderSchoolName",
                    SystemConstant.RESOURCE_PREFIX + QueryParser.escape(keyword) + SystemConstant.RESOURCE_SUFFIX));
            builder.must(b);
        } else {
            if (type == 1) {
                BoolQueryBuilder b = new BoolQueryBuilder();
                b.should(new MatchQueryBuilder("title", keyword).type(Type.PHRASE));
                b.should(new MatchQueryBuilder("uploaderName", keyword).type(Type.PHRASE));
                b.should(new MatchQueryBuilder("uploaderSchoolName", keyword).type(Type.PHRASE));
                builder.must(b);
            } else if (type == 2) {
                BoolQueryBuilder b = new BoolQueryBuilder();
                b.should(new MatchQueryBuilder("title", keyword));
                b.should(new MatchQueryBuilder("uploaderName", keyword));
                b.should(new MatchQueryBuilder("uploaderSchoolName", keyword));
                builder.must(b);
            } else if (type == 4) {
                BoolQueryBuilder b = new BoolQueryBuilder();
                b.should(new RegexpQueryBuilder("title",
                        SystemConstant.RESOURCE_PREFIX + QueryParser.escape(keyword) + SystemConstant.RESOURCE_SUFFIX));
                b.should(new RegexpQueryBuilder("uploaderName",
                        SystemConstant.RESOURCE_PREFIX + QueryParser.escape(keyword) + SystemConstant.RESOURCE_SUFFIX));
                b.should(new RegexpQueryBuilder("uploaderSchoolName",
                        SystemConstant.RESOURCE_PREFIX + QueryParser.escape(keyword) + SystemConstant.RESOURCE_SUFFIX));
                builder.must(b);
            } else {
                BoolQueryBuilder keywordBoolQueryBuilder = new BoolQueryBuilder();
                String[] key = keyword.split("");
                for (String c : key) {
                    keywordBoolQueryBuilder.should(new RegexpQueryBuilder("title",
                            SystemConstant.RESOURCE_PREFIX + QueryParser.escape(c) + SystemConstant.RESOURCE_SUFFIX));
                    keywordBoolQueryBuilder.should(new RegexpQueryBuilder("uploaderName",
                            SystemConstant.RESOURCE_PREFIX + QueryParser.escape(c) + SystemConstant.RESOURCE_SUFFIX));
                    keywordBoolQueryBuilder.should(new RegexpQueryBuilder("uploaderSchoolName",
                            SystemConstant.RESOURCE_PREFIX + QueryParser.escape(c) + SystemConstant.RESOURCE_SUFFIX));
                }
                builder.must(keywordBoolQueryBuilder);
            }
        }
    }

    @Override
    public Page<ElasticYxyResResource> getPageKeyword(BoolQueryBuilder maxBoolQueryBuilder, String keyword,
            Integer keywordType, Pageable pageable) {
        BoolQueryBuilder b1 = new BoolQueryBuilder();
        b1.must(maxBoolQueryBuilder);
        Page<ElasticYxyResResource> page = null;
        if (StringUtils.isNotBlank(keyword)) {
            if (keywordType == 3) {
                keywordTUS(keyword, 1, b1);
            } else if (keywordType == 2) {
                keywordTU(keyword, 1, b1);
            }
            page = getPage(b1, pageable);
            //
            if (page.getContent().size() == 0) {
                BoolQueryBuilder b2 = new BoolQueryBuilder();
                b2.must(maxBoolQueryBuilder);
                if (keywordType == 3) {
                    keywordTUS(keyword, 2, b2);
                } else if (keywordType == 2) {
                    keywordTU(keyword, 2, b2);
                }
                page = getPage(b2, pageable);
            }
            if (page.getContent().size() == 0) {
                BoolQueryBuilder b4 = new BoolQueryBuilder();
                b4.must(maxBoolQueryBuilder);
                if (keywordType == 3) {
                    keywordTUS(keyword, 4, b4);
                } else if (keywordType == 2) {
                    keywordTU(keyword, 4, b4);
                }
                page = getPage(b4, pageable);
            }
            if (page.getContent().size() == 0) {
                BoolQueryBuilder b3 = new BoolQueryBuilder();
                b3.must(maxBoolQueryBuilder);
                if (keywordType == 3) {
                    keywordTUS(keyword, 3, b3);
                } else if (keywordType == 2) {
                    keywordTU(keyword, 3, b3);
                }
                page = getPage(b3, pageable);
            }
        } else {
            page = getPage(b1, pageable);
        }
        return page;
    }

    @Override
    public Page<ElasticYxyResResource> getPage(BoolQueryBuilder boolQueryBuilder, Pageable pageable) {
        SearchQuery query = new NativeSearchQueryBuilder().withPageable(pageable).withQuery(boolQueryBuilder).build();
        Page<ElasticYxyResResource> page = et.queryForPage(query, ElasticYxyResResource.class);
        return page;
    }

    @Override
    public long countResource(BoolQueryBuilder queryBuilder) {
        SearchQuery query = new NativeSearchQueryBuilder().withQuery(queryBuilder).build();
        long count = et.count(query, ElasticYxyResResource.class);
        return count;
    }

}
